package com.teamone.etl.enterprise.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamone.etl.enterprise.dao.AlarmPhoneDao;
import com.teamone.etl.enterprise.entity.AlarmPhone;
import com.teamone.etl.enterprise.service.AlarmPhoneService;
import com.teamone.etl.exception.CommonCode;
import com.teamone.etl.exception.CustomException;
import com.teamone.etl.util.DesensitizationUtilPlus;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.teamone.etl.constant.EtlConstant.Number.*;

/**
 预警号码(AlarmPhone)表服务实现类

 @author makejava
 @since 2021-01-21 11:30:12 */
@Service("alarmPhoneService")
@Transactional(rollbackFor = Exception.class)
public class AlarmPhoneServiceImpl extends ServiceImpl<AlarmPhoneDao, AlarmPhone> implements AlarmPhoneService {

    @Override
    public List<AlarmPhone> getList (Integer enterpriseId) {
        List<AlarmPhone> list = baseMapper.getList(enterpriseId);
        list.forEach(alarmPhone -> alarmPhone.setPhone(DesensitizationUtilPlus.mobileEncrypt(alarmPhone.getPhone())));
        return list;
    }

    @Override
    public int add (AlarmPhone alarmPhone) throws CustomException {
        alarmPhone.verify();
        if(baseMapper.isExsists(alarmPhone) > NONE){
            throw new CustomException(CommonCode.MOBILE_ALREADY_EXSISTS);
        }
        return baseMapper.add(alarmPhone);
    }

    @Override
    public int delete (Integer id) {
        return baseMapper.deleteByIdWithSql(id);
    }

}
